package csu.rjxyg.action.infoAction;

import java.util.ArrayList;
import java.util.List;

import csu.rjxyg.action.BaseAction;
import csu.rjxyg.action.constant.InfoConstant;
import csu.rjxyg.action.constant.UserConstant;
import csu.rjxyg.bean.core.BaseBean;
import csu.rjxyg.bean.core.BaseObject;
import csu.rjxyg.factory.core.UtilFactory;
import csu.rjxyg.service.BaseInfoService;
import csu.rjxyg.service.BaseUserService;
import csu.rjxyg.util.LogUtil;
import csu.rjxyg.util.TimeUtil;

public class SendInfoAction extends BaseAction{
	private LogUtil log;	
	private TimeUtil time;
	
	private String infoAcceptUserId;
	private String infoAcceptUserName;
	private String infoName;
	private String infoBody;
	
	public SendInfoAction(){
		log = UtilFactory.getLogUtil(this.getClass());
		time = UtilFactory.getTimeUtil();
	}
	
	public void SendInfo(){
		String name = null;
		try{
			BaseObject sendObject = (BaseObject) this.getSession().get(UserConstant.USER_SESSION);
			name = (String) sendObject.get(UserConstant.USER_ID);
			
			BaseUserService userService = (BaseUserService) factory.getBean(this.USER_SERVICE);
			BaseObject acceptObject;
			if( infoAcceptUserId.equals(name) ){
				acceptObject = sendObject;
			}else{
				acceptObject = userService.getBeanInfoById(infoAcceptUserId);
			}
			
			
			
			if( acceptObject == null ){
				throw new Exception();
			}
			
			BaseObject infoBean = (BaseObject) factory.getBean(BaseBean.BEAN_OBJECT);
			infoBean.set(InfoConstant.INFO_SEND_USER_ID, name);
			infoBean.set(InfoConstant.INFO_SEND_USER_NAME, sendObject.get(UserConstant.USER_NAME));
			
			infoBean.set(InfoConstant.INFO_ACCEPT_USER_ID, acceptObject.get(UserConstant.USER_ID));
			infoBean.set(InfoConstant.INFO_ACCEPT_USER_NAME, acceptObject.get(UserConstant.USER_NAME));
			infoBean.set(InfoConstant.INFO_NAME, infoName);
			infoBean.set(InfoConstant.INFO_BODY, infoBody);
			infoBean.set(InfoConstant.INFO_STATUS, "未读");
			infoBean.set(InfoConstant.INFO_TIME, time.getNowTime());
			
			infoBean.set(InfoConstant.INFO_USER_NUMBER, 2);
			
			BaseInfoService infoService = (BaseInfoService) factory.getBean(this.INFO_SERVICE);
			if( infoService.insertInfo(infoBean) ){
				String infoId = (String) infoBean.get(InfoConstant.INFO_ID);
				List sendList = (List) sendObject.get(UserConstant.USER_SEND_INFO);
				if( sendList == null ){
					sendList = new ArrayList();
				}
				sendList.add(infoId);
				sendObject.set(UserConstant.USER_SEND_INFO, sendList);
				userService.updateUserBean(name, sendObject);
				
				List acceptList = (List) acceptObject.get(UserConstant.USER_ACCEPT_INFO);
				if( acceptList == null ){
					acceptList = new ArrayList();
				}
				acceptList.add(infoId);
				acceptObject.set(UserConstant.USER_ACCEPT_INFO, acceptList);
				userService.updateUserBean((String) acceptObject.get(UserConstant.USER_ID), acceptObject);
			}
			log.info(name + "发送信息成功");
		}catch(Exception e){
			log.info(name + "发送信息失败");
			e.printStackTrace();
			log.info(e.getMessage());
		}
	}

	public String getInfoAcceptUserName() {
		return infoAcceptUserName;
	}

	public void setInfoAcceptUserName(String infoAcceptUserName) {
		this.infoAcceptUserName = infoAcceptUserName;
	}

	public String getInfoName() {
		return infoName;
	}

	public void setInfoName(String infoName) {
		this.infoName = infoName;
	}

	public String getInfoBody() {
		return infoBody;
	}

	public void setInfoBody(String infoBody) {
		this.infoBody = infoBody;
	}

	public String getInfoAcceptUserId() {
		return infoAcceptUserId;
	}

	public void setInfoAcceptUserId(String infoAcceptUserId) {
		this.infoAcceptUserId = infoAcceptUserId;
	}
	
	
}
